package com.kedsoft.andaily.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.kedsoft.andaily.common.CommonResult;
import com.kedsoft.andaily.entity.Cunsumer;
import com.kedsoft.andaily.entity.CunsumerHisPos;
import com.kedsoft.andaily.entity.CunsumerPos;
import com.kedsoft.andaily.service.CunsumerHisPosService;
import com.kedsoft.andaily.service.CunsumerPosService;
import com.kedsoft.andaily.service.CunsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api("位置管理")
@RestController
@RequestMapping(value = "/api/pos")
public class CunsumerPositionApiController {
    @Autowired
    CunsumerPosService cunsumerPosService;

    @Autowired
    CunsumerHisPosService cunsumerHisPosService;

    @Autowired
    CunsumerService cunsumerService;

    @ApiOperation(value = "上传位置")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cunsumerlng", dataType = "double", required = true, value = "经度"),
            @ApiImplicitParam(paramType = "query", name = "cunsumerlat", dataType = "double", required = true, value = "纬度")
    })
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public CommonResult<String> getTask(@CookieValue("userName") String userName, @RequestParam("cunsumerlng") double cunsumerlng, @RequestParam("cunsumerlat") double cunsumerlat)
    {
        Cunsumer cunsumer = cunsumerService.getCunsumerByMobile(userName);

        CunsumerHisPos hisPos = new CunsumerHisPos();
        hisPos.setCunsumerid(cunsumer.getId());
        hisPos.setCunsumerlat(cunsumerlat);
        hisPos.setCunsumername(cunsumer.getName());
        hisPos.setCunsumerlng(cunsumerlng);
        hisPos.setUptime(new Date());
        cunsumerHisPosService.insert(hisPos);

        EntityWrapper<CunsumerPos> wrapper = new EntityWrapper<>();
        wrapper.eq("cunsumerid", cunsumer.getId());

        CunsumerPos pos = cunsumerPosService.selectOne(wrapper);
        if(pos == null)
        {
            pos = new CunsumerPos();
        }
        pos.setCunsumerid(cunsumer.getId());
        pos.setCunsumerlat(cunsumerlat);
        pos.setCunsumername(cunsumer.getName());
        pos.setCunsumerlng(cunsumerlng);
        pos.setUptime(new Date());
        cunsumerPosService.insertOrUpdate(pos);
        return CommonResult.success("上传位置成功");
    }

    @ApiOperation(value = "获取巡视单位人员位置")
    @RequestMapping(value = "/getCunsumerPos", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "routerId", dataType = "int", required = true, value = "巡视单位ID"),
    })
    public CommonResult<List<CunsumerPos>> getCunsumerPosByRouterId(@RequestParam("routerId") int id)
    {
        EntityWrapper<Cunsumer> wrapper = new EntityWrapper<>();
        wrapper.eq("routerid", id);
        wrapper.eq("status", 1);
        List<Cunsumer> cunsumers = cunsumerService.selectList(wrapper);
        List<String> cunsumerIds = new ArrayList<>();
        for(Cunsumer cunsumer : cunsumers)
        {
            cunsumerIds.add("" + cunsumer.getId());
        }
        EntityWrapper<CunsumerPos> wrapper2 = new EntityWrapper<>();
        wrapper2.in("cunsumerid", cunsumerIds);
        return CommonResult.success(cunsumerPosService.selectList(wrapper2));
    }
}
